package com.example.lianjuan.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.lianjuan.Base.IlinearInfo;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.UtilsUrl;

import java.util.ArrayList;
import java.util.List;

public class LinearItemContentAdapter extends RecyclerView.Adapter<LinearItemContentAdapter.InnerHolder> {
    List<IlinearInfo> data = new ArrayList<>();
    private OnItemClickListener onItemClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View mItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_pager_content, parent, false);
        return new InnerHolder(mItemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        LogUtils.d(this, "onBindViewHolder----" + position);
        IlinearInfo dataDTO = data.get(position);
        holder.setData(dataDTO);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(dataDTO);
                }
            }
        });
    }

    public void setData(List<? extends IlinearInfo> data) {
        if (data == null) {
            return;
        }
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /**
     * 添加加载更多数据
     *
     * @param data
     */
    public void addMoreData(List<? extends IlinearInfo> data) {
        //原来的数据大小
        int originalSize = this.data.size();
        this.data.addAll(data);
        //只刷新指定位子的数据,到结束的数据
        LogUtils.d(this, originalSize + "=======" + this.data.size());
        notifyItemRangeChanged(originalSize, this.data.size());
        LogUtils.d(this, "刷新成功");
    }

    public void addReleaseData(List<? extends IlinearInfo> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
        LogUtils.d(this, "刷新成功");
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        private ImageView cover;
        private TextView title, offPriceTv, finalPriceTv, sellCountTv, originalPriseTv;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.goods_cover);
            title = itemView.findViewById(R.id.goods_title);
            offPriceTv = itemView.findViewById(R.id.goods_off_prise);
            finalPriceTv = itemView.findViewById(R.id.goods_after_off_prise);
            sellCountTv = itemView.findViewById(R.id.goods_sell_count);
            originalPriseTv = itemView.findViewById(R.id.goods_original_prise);
        }

        public void setData(IlinearInfo dataDTO) {
            title.setText(dataDTO.getTitle());
            Glide.with(itemView.getContext()).load(UtilsUrl.getCoverPath(dataDTO.getPicUrl(), 80)).into(cover);
            //优惠多少
            long couponAmount = dataDTO.getCoupon();
            offPriceTv.setText(String.format(itemView.getContext().getString(R.string.goods_off_price), couponAmount));
            //优惠后的价格
            Float finalPrice = Float.parseFloat(dataDTO.getFinalPrice());
            //LogUtils.d(this, "finalPrice---->" + finalPrice);
            finalPriceTv.setText(String.format("劵后价：%.2f", finalPrice));
            //原价
            Float sellCount = (Float) (couponAmount + finalPrice);
            //中划线
            originalPriseTv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            originalPriseTv.setText(String.format("¥%.1f", sellCount));
            //购买人数
            sellCountTv.setText(String.format(itemView.getContext().getString(R.string.volume), dataDTO.getVolume()));
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(IlinearInfo iBaseInfo);
    }
}
